package entity

import (
	"time"
	"xr-game-server/core/constants/db"
	"xr-game-server/core/migrate"
	"xr-game-server/core/snowflake"
	"xr-game-server/core/syndb"
)

const (
	// TbGuildApplication 公会申请表名
	TbGuildApplication db.TbName = "guild_applications"
)

// 公会申请表列名
const (
	GuildAppGuildId db.TbCol = "guild_id"
	GuildAppRoleId  db.TbCol = "role_id"
)

// GuildApplication 公会申请实体
type GuildApplication struct {
	migrate.MoreModel
	GuildId uint64 `gorm:"comment:公会ID"`
	RoleId  uint64 `gorm:"comment:申请角色ID"`
}

// 公会相关常量
const (
	GuildMemberTitlePresident = 1 // 会长
	GuildMemberTitleOfficer   = 2 // 官员
	GuildMemberTitleMember    = 3 // 普通成员

)

// GuildApplication setters

func (ga *GuildApplication) SetRoleId(roleId uint64) {
	ga.RoleId = roleId
	ga.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuildApplication, GuildAppRoleId, &syndb.ColData{
		IdVal:  ga.ID,
		ColVal: roleId,
	})
}
func (ga *GuildApplication) SetGuildId(guildId uint64) {
	ga.GuildId = guildId
	ga.SetUpdatedAt(time.Now())
	syndb.AddDataToLazyChan(TbGuildApplication, GuildAppGuildId, &syndb.ColData{
		IdVal:  ga.ID,
		ColVal: guildId,
	})
}

func (ga *GuildApplication) SetCreatedAt(time time.Time) {
	ga.CreatedAt = time
	syndb.AddDataToLazyChan(TbGuildApplication, db.CreatedAtName, &syndb.ColData{
		IdVal:  ga.ID,
		ColVal: time,
	})
}

func (ga *GuildApplication) SetUpdatedAt(time time.Time) {
	ga.UpdatedAt = time
	syndb.AddDataToLazyChan(TbGuildApplication, db.UpdatedAtName, &syndb.ColData{
		IdVal:  ga.ID,
		ColVal: time,
	})
}
func (ga *GuildApplication) SetIsDeleted(deleted bool) {
	ga.IsDeleted = deleted
	now := time.Now()
	ga.SetDeletedAt(&now)
	ga.SetUpdatedAt(now)
	syndb.AddDataToLazyChan(TbGuildApplication, db.IsDeletedName, &syndb.ColData{
		IdVal:  ga.ID,
		ColVal: deleted,
	})
}
func (ga *GuildApplication) SetDeletedAt(time *time.Time) {
	ga.DeletedAt = *time
	syndb.AddDataToLazyChan(TbGuildApplication, db.DeletedAtName, &syndb.ColData{
		IdVal:  ga.ID,
		ColVal: time,
	})
}

func initGuildApplication() {
	// 注册GuildApplication表
	syndb.RegLazyWithMiddle(TbGuildApplication, db.CreatedAtName)
	syndb.RegLazyWithMiddle(TbGuildApplication, db.UpdatedAtName)
	syndb.RegLazyWithMiddle(TbGuildApplication, db.IsDeletedName)
	syndb.RegLazyWithMiddle(TbGuildApplication, db.DeletedAtName)

	syndb.RegLazyWithMiddle(TbGuildApplication, GuildAppGuildId)
	syndb.RegLazyWithMiddle(TbGuildApplication, GuildAppRoleId)
	migrate.AutoMigrate(&GuildApplication{})
}

// NewGuildApplication 创建公会申请
func NewGuildApplication(guildId uint64, roleId uint64) *GuildApplication {
	ret := &GuildApplication{}

	ret.ID = snowflake.GetId()
	ret.SetGuildId(guildId)
	ret.SetRoleId(roleId)
	ret.SetCreatedAt(time.Now())
	ret.SetUpdatedAt(time.Now())

	return ret
}
